package com.cd.servlet;

import cn.hutool.json.JSONUtil;
import com.cd.dao.TVDao;
import com.cd.entity.TV;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet("/tv")
public class TVServlet extends HttpServlet {
    private TVDao tvDao;

    @Override
    public void init() throws ServletException {
        tvDao = new TVDao();
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf-8");
        req.setCharacterEncoding("utf-8");

        String action = req.getParameter("action");
        if (action == null) {
            action = "list";
        }

        try {
            switch (action) {
                case "list":
                    // 查询所有电视剧
                    List<TV> tvList = tvDao.findAll();
                    resp.getWriter().write(JSONUtil.toJsonStr(tvList));
                    break;
                case "listWithCategory":
                    // 查询所有电视剧及其分类信息
                    List<TV> tvListWithCategory = tvDao.findAllWithCategory();
                    resp.getWriter().write(JSONUtil.toJsonStr(tvListWithCategory));
                    break;
                case "add":
                    // 添加电视剧
                    TV tv = new TV();
                    tv.setDramaName(req.getParameter("dramaName"));
                    tv.setDirector(req.getParameter("director"));
                    tv.setScreenwriter(req.getParameter("screenwriter"));
                    tv.setLeadActors(req.getParameter("leadActors"));

                    // 处理日期
                    String releaseYearStr = req.getParameter("releaseYear");
                    if (releaseYearStr != null && !"".equals(releaseYearStr)) {
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        Date releaseYear = sdf.parse(releaseYearStr);
                        tv.setReleaseYear(releaseYear);
                    }

                    tv.setEpisodeCount(Integer.valueOf(req.getParameter("episodeCount")));
                    tv.setScore(Double.valueOf(req.getParameter("score")));
                    tv.setCategoryId(Integer.valueOf(req.getParameter("categoryId")));
                    tv.setDescription(req.getParameter("description"));
                    tv.setPosterUrl(req.getParameter("posterUrl"));

                    int addResult = tvDao.insert(tv);
                    resp.getWriter().write("{\"result\":" + addResult + "}");
                    break;
                case "update":
                    // 更新电视剧
                    TV updateTV = new TV();
                    updateTV.setTvId(Integer.valueOf(req.getParameter("tvId")));
                    updateTV.setDramaName(req.getParameter("dramaName"));
                    updateTV.setDirector(req.getParameter("director"));
                    updateTV.setScreenwriter(req.getParameter("screenwriter"));
                    updateTV.setLeadActors(req.getParameter("leadActors"));

                    // 处理日期
                    String updateReleaseYearStr = req.getParameter("releaseYear");
                    if (updateReleaseYearStr != null && !"".equals(updateReleaseYearStr)) {
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        Date releaseYear = sdf.parse(updateReleaseYearStr);
                        updateTV.setReleaseYear(releaseYear);
                    }

                    updateTV.setEpisodeCount(Integer.valueOf(req.getParameter("episodeCount")));
                    updateTV.setScore(Double.valueOf(req.getParameter("score")));
                    updateTV.setCategoryId(Integer.valueOf(req.getParameter("categoryId")));
                    updateTV.setDescription(req.getParameter("description"));
                    updateTV.setPosterUrl(req.getParameter("posterUrl"));

                    int updateResult = tvDao.update(updateTV);
                    resp.getWriter().write("{\"result\":" + updateResult + "}");
                    break;
                case "delete":
                    // 删除电视剧
                    int tvId = Integer.valueOf(req.getParameter("tvId"));
                    int deleteResult = tvDao.delete(tvId);
                    resp.getWriter().write("{\"result\":" + deleteResult + "}");
                    break;
                default:
                    resp.getWriter().write("{\"error\":\"未知操作\"}");
            }
        } catch (ParseException e) {
            e.printStackTrace();
            resp.getWriter().write("{\"error\":\"日期格式错误\"}");
        } catch (Exception e) {
            e.printStackTrace();
            resp.getWriter().write("{\"error\":\"服务器内部错误\"}");
        } finally {
            resp.getWriter().close();
        }
    }
}